architecture := x86_64
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
GOARM := 7
cat := $(if $(filter $(OS),sh.exe),type,cat)
version := $(shell $(cat) build_version.txt)
rpm_version := $(version)-1
rpm_iteration := 1
pkg := rpm



.PHONY: all clean rpm

all: lifi
.PHONY:build_bin
build_bin:
	@echo "编译cmd/main.go $(GOOS)-$(GOARCH)"
	@echo $(DESTDIR)
	@mkdir -pv $(DESTDIR)
	#CGO_ENABLED=1 CGO_LDFLAGS="-L/usr/lib " go build  -race  -trimpath -o $(DESTDIR)/usr/bin/lifi -ldflags '$(LDFLAGS)' ./cmd

	go build    -trimpath -o $(DESTDIR)/usr/bin/lifi -ldflags '$(LDFLAGS)' ./cmd
	#go build    -trimpath -o lifi2 -ldflags '-w -s' ./cmd

	#CGO_ENABLED=0 CC="gcc" CXX="g++"  go build  -o $(DESTDIR)/usr/bin/lifi   -ldflags '$(LDFLAGS)' ./cmd
	#CGO_ENABLED=1 CC="x86_64-linux-musl-gcc" CXX="x86_64-linux-musl-g++" go build -v -race -n -o $(DESTDIR)/usr/bin/lifi -ldflags "$(LDFLAGS)" ./cmd
	# CGO_ENABLED=1 go build -o $(DESTDIR)/usr/bin/lifi -ldflags "$(LDFLAGS)" ./cmd
	#go env
	#CGO_ENABLED=1 EXT_LD="/usr/local/bin/x86_64-linux-musl-ld" CC="x86_64-linux-musl-gcc" CXX="x86_64-linux-musl-g++" CGO_CFLAGS="-I/usr/local/include "  CGO_LDFLAGS="-L/usr/local/lib " go build -compiler gc -v -o $(DESTDIR)/usr/bin/lifi   -ldflags '$(LDFLAGS)' /Users/fuqiangnian/workspace/go/lifi/cmd
	#CGO_ENABLED=1 CC="x86_64-linux-musl-gcc" CXX="x86_64-linux-musl-g++" CGO_CFLAGS="-I/usr/local/include "   go env
	#CGO_ENABLED=1  	CC="x86_64-linux-musl-gcc" CXX="x86_64-linux-musl-g++" 	CGO_CFLAGS="-I/usr/local/include -O2 -g -lm -Wall"  CGO_CPPFLAGS='-lm' 	CGO_LDFLAGS=" -O2 -g" 	go build -trimpath -compiler gc -v -x -o $(DESTDIR)/usr/bin/lifi 	-gcflags  "-w " 	-gccgoflags  "-lm " 	-ldflags '$(LDFLAGS)' /Users/fuqiangnian/workspace/go/lifi/cmd

lifi: build_bin
	@echo success bin file lifi

rpm: lifi
	@mkdir -pv build/dist
	@mkdir -pv ${DESTDIR}/etc/systemd/system/
	@mkdir -pv ${DESTDIR}/etc/cphm/lifi/
	cp -f scripts/lifi.service ${DESTDIR}/etc/systemd/system/lifi.service
	cp -f cmd/config.yaml ${DESTDIR}/etc/cphm/lifi/config.yaml
	cp -f cmd/point-mapping.json ${DESTDIR}/etc/cphm/lifi/point-mapping.json

	fpm --force \
    	--log info \
    	--architecture $(architecture) \
    	--input-type dir \
    	--output-type rpm \
    	--vendor xjgyy \
    	--url https://git.xj-icbd.com/cphm/lifi \
    	--license MIT \
    	--maintainer fuqiangnian@goldwind.com \
    	--config-files etc/cphm/lifi/config.yaml \
    	--config-files etc/cphm/lifi/point-mapping.json \
    	--config-files etc/systemd/system/lifi.service \
    	--after-install scripts/rpm/post-install.sh \
    	--before-install scripts/rpm/pre-install.sh \
    	--after-remove scripts/rpm/post-remove.sh \
    	--description "data pipeline for iot" \
    	--depends coreutils \
    	--depends shadow-utils \
    	--rpm-digest sha256 \
    	--rpm-posttrans scripts/rpm/post-install.sh \
    	--rpm-os ${GOOS} \
    	--name lifi \
    	--version $(version) \
    	--iteration $(rpm_iteration) \
    	--chdir $(DESTDIR) \
    	--rpm-defattrdir 0755:cphm:cphm \
    	--rpm-defattrfile 0644:cphm:cphm \
    	--package build/dist/lifi-$(rpm_version).$(architecture).${pkg} ;


deb: build_bin
	@mkdir -pv build/dist
	@mkdir -pv ${DESTDIR}/etc/systemd/system/
	@mkdir -pv ${DESTDIR}/etc/cphm/lifi/
	cp -f scripts/lifi.service ${DESTDIR}/etc/systemd/system/lifi.service
	cp -f cmd/config.yaml ${DESTDIR}/etc/cphm/lifi/config.yaml
	cp -f cmd/point-mapping.json ${DESTDIR}/etc/cphm/lifi/point-mapping.json

	fpm --force \
    	--log info \
    	--architecture $(architecture) \
    	--input-type dir \
    	--output-type deb \
    	--vendor xjgyy \
    	--url https://git.xj-icbd.com/cphm/lifi \
    	--license MIT \
    	--maintainer fuqiangnian@goldwind.com \
    	--config-files etc/cphm/lifi/config.yaml \
    	--config-files etc/cphm/lifi/point-mapping.json \
    	--config-files etc/systemd/system/lifi.service \
    	--after-install scripts/rpm/post-install.sh \
    	--before-install scripts/rpm/pre-install.sh \
    	--after-remove scripts/rpm/post-remove.sh \
    	--description "data pipeline for iot" \
    	--name lifi \
    	--version $(version) \
    	--iteration $(rpm_iteration) \
    	--chdir $(DESTDIR) \
    	--package build/dist/lifi-$(rpm_version).$(architecture).${pkg} ;


clean:
	rm -fr build

deb : pkg=deb
rpm : pkg=rpm
deb rpm clean: export DESTDIR = build/$(GOOS)-$(GOARCH)-$(pkg)/lifi-$(version)
#lifi rpm clean: export LDFLAGS = -L /usr/local/lib -w -s -extldflags "-static -fpic"
deb rpm clean: export LDFLAGS =-w -s
deb rpm clean: export GOOS := linux
deb rpm clean: export GOARCH := amd64
